﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class HangHoaDAO
    {
        #region 1.Retrieving
        public static List<HangHoaDTO> SelectAllHangHoa()
        {
            List<HangHoaDTO> listHangHoa = new List<HangHoaDTO>();

            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectHangHoasAll");
            foreach (DataRow dr in dt.Rows)
            {
                HangHoaDTO hangHoa = new HangHoaDTO();
                hangHoa.DonGia = float.Parse(dr["DonGia"].ToString());
                hangHoa.MaDonViTinh = dr["MaDonViTinh"].ToString();
                hangHoa.MaMatHang = dr["MaMatHang"].ToString();
                hangHoa.TenMatHang = dr["TenMatHang"].ToString();
                hangHoa.SoLuongTon = int.Parse(dr["SoLuongTon"].ToString());

                listHangHoa.Add(hangHoa);

            }

            return listHangHoa;
        }
        public static List<HangHoaVaDonViTinhDTO> SelectAllHangHoaAndDonViTinh()
        {
            List<HangHoaVaDonViTinhDTO> listHangHoa = new List<HangHoaVaDonViTinhDTO>();

            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_KetBangHangHoaAndDonViTinh");
            foreach (DataRow dr in dt.Rows)
            {
                HangHoaVaDonViTinhDTO hangHoa = new HangHoaVaDonViTinhDTO();
                hangHoa.DonGia = float.Parse(dr["DonGia"].ToString());
                hangHoa.MaDonViTinh = dr["MaDonViTinh"].ToString();
                hangHoa.MaMatHang = dr["MaMatHang"].ToString();
                hangHoa.TenMatHang = dr["TenMatHang"].ToString();
                hangHoa.TenDonViTinh = dr["TenDonViTinh"].ToString();
                hangHoa.SoLuongTon = int.Parse(dr["SoLuongTon"].ToString());
                listHangHoa.Add(hangHoa);

            }

            return listHangHoa;
        }
        public static bool CheckMaHangHoaExits(string maHangHoa)
        {
            List<HangHoaDTO> listHangHoa = new List<HangHoaDTO>();

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaMatHang", maHangHoa));

            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectHangHoasByAndMaMatHang", listSqlParam);
            foreach (DataRow dr in dt.Rows)
            {
                HangHoaDTO hangHoa = new HangHoaDTO();
                hangHoa.DonGia = float.Parse(dr["DonGia"].ToString());
                hangHoa.MaDonViTinh = dr["MaDonViTinh"].ToString();
                hangHoa.MaMatHang = dr["MaMatHang"].ToString();
                hangHoa.TenMatHang = dr["TenMatHang"].ToString();
                hangHoa.SoLuongTon = int.Parse(dr["SoLuongTon"].ToString());

                listHangHoa.Add(hangHoa);

            }

            if (listHangHoa.Count == 1)
            {
                return true;
            }
            else
                return false;
        }

        public static List<HangHoaDTO> SelectHangHoaByID(string maHangHoa)
        {
            List<HangHoaDTO> listHangHoa = new List<HangHoaDTO>();

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaMatHang", maHangHoa));

            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectHangHoasByAndMaMatHang", listSqlParam);
            foreach (DataRow dr in dt.Rows)
            {
                HangHoaDTO hangHoa = new HangHoaDTO();
                hangHoa.DonGia = float.Parse(dr["DonGia"].ToString());
                hangHoa.MaDonViTinh = dr["MaDonViTinh"].ToString();
                hangHoa.MaMatHang = dr["MaMatHang"].ToString();
                hangHoa.TenMatHang = dr["TenMatHang"].ToString();
                hangHoa.SoLuongTon = int.Parse(dr["SoLuongTon"].ToString());

                listHangHoa.Add(hangHoa);

            }

            return listHangHoa;
        }
        #endregion
        #region 2.Deleting
        public static bool DeleteHangHoa(string maHangHoa)
        {


            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaMatHang", maHangHoa));
            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_DeleteHangHoa", listSqlParam);
            if (n == -1)
            {
                return true;
            }
            else
                return false;

        }
        #endregion
        #region 3.Inserting
        public static bool InsertHangHoa(HangHoaDTO hangHoa)
        {

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaMatHang", hangHoa.MaMatHang));
            listSqlParam.Add(new SqlParameter("@TenMatHang", hangHoa.TenMatHang));
            listSqlParam.Add(new SqlParameter("@DonGia", hangHoa.DonGia));
            listSqlParam.Add(new SqlParameter("@MaDonViTinh", hangHoa.MaDonViTinh));
            listSqlParam.Add(new SqlParameter("@SoLuongTon", hangHoa.SoLuongTon));

            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_InsertHangHoa", listSqlParam);
            if (n == -1)
            {
                return true;
            }
            else
                return false;

        }
        #endregion
        #region 4.Updating
        public static bool UpdateHangHoa(HangHoaDTO hangHoa)
        {

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaMatHang", hangHoa.MaMatHang));
            listSqlParam.Add(new SqlParameter("@TenMatHang", hangHoa.TenMatHang));
            listSqlParam.Add(new SqlParameter("@DonGia", hangHoa.DonGia));
            listSqlParam.Add(new SqlParameter("@MaDonViTinh", hangHoa.MaDonViTinh));
            listSqlParam.Add(new SqlParameter("@SoLuongTon", hangHoa.SoLuongTon));

            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_UpdateHangHoa", listSqlParam);

            if (n == -1)
            {
                return true;
            }
            else
                return false;

        }
        #endregion
    }
}
